package com.xk.springboot.interceptor;

import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * 登陆拦截器
 * @author:Teacher黄
 * @date:Created at 2020/04/10
 */
public class LoginInterceptor implements HandlerInterceptor {


    /**
     * 手动去重写preHandler
     * @param request
     * @param response
     * @param handler
     * @return
     * @throws Exception
     */
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        System.out.println("############拦截到了!##############");
        /**
         * 判断是否有session的值, 有就放行, 没有就拦截=> 跳转到登陆页面
         */
        HttpSession session = request.getSession();
        if(session.getAttribute("member") == null){
            // 说明没有登陆
            // 重定向到登陆页面
            response.sendRedirect("/login/page");
            return false;
        }
        // 放行 返回true  不放行返回false
        return true;
    }
}
